package com.example.myanimator;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BaseInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myanimator.evaluator.CharEvaluator;
import com.example.myanimator.evaluator.FallingBallEvaluator;
import com.example.myanimator.evaluator.FallingImageView;
import com.example.myanimator.evaluator.MyEvaluator;
import com.example.myanimator.evaluator.ReverseEvaluator;
import com.example.myanimator.view.CircleImageView;
import com.example.myanimator.view.CustomCircleViewCanvas;
import com.example.myanimator.view.DrawCanvas;

public class FourActivity extends AppCompatActivity {

    private TextView tv2;
    private TextView tv_3;
    private CircleImageView tv_4;
    private FallingImageView fallingImageView;
    private ValueAnimator valueAnimator2;
    private ValueAnimator valueAnimator3;
    private ValueAnimator valueAnimator4;
    private ObjectAnimator objectAnimator;
    private ObjectAnimator objectAnimator1;
    private PropertyValuesHolder propertyValuesHolder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_four);
        tv2 = findViewById(R.id.tv_2);
        tv_3 = findViewById(R.id.tv_3);
        tv_4 = findViewById(R.id.leke_group_space_civ_header);
        fallingImageView = findViewById(R.id.fall_image);
        initEva2();

        initEva3();
        initEva4();
        initEva5();
        initEva6();
        findViewById(R.id.tv_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                valueAnimator2.start();
//                valueAnimator3.start();
                //valueAnimator4 和 objectAnimator 和 objectAnimator1效果是一样的
                valueAnimator4.start();
//                objectAnimator.start();
//                objectAnimator1.start();
            }
        });
    }

    private void initEva3() {
        valueAnimator3 = ValueAnimator.ofObject(new CharEvaluator(),new Character('A'),new Character('Z'));

        valueAnimator3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                char charValue = (char) animation.getAnimatedValue();
                tv_3.setText(String.valueOf(charValue));
            }
        });
        valueAnimator3.setEvaluator(new CharEvaluator());
        valueAnimator3.setInterpolator(new AccelerateInterpolator());
        valueAnimator3.setDuration(3000);
    }

    private void initEva4() {
        valueAnimator4 = ValueAnimator.ofObject(new FallingBallEvaluator(),new Point(0,0),new Point(500,500));

        valueAnimator4.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Point currentValue = (Point) animation.getAnimatedValue();
                tv_4.layout(currentValue.x,currentValue.y,currentValue.x+tv_4.getWidth(),currentValue.y+tv_4.getHeight());
            }
        });
        valueAnimator4.setStartDelay(4000);
        valueAnimator4.setEvaluator(new FallingBallEvaluator());
        valueAnimator4.setInterpolator(new AccelerateInterpolator());
        valueAnimator4.setDuration(3000);
    }
    private void initEva5() {
        objectAnimator = ObjectAnimator.ofObject(fallingImageView,"fallingPos",new FallingBallEvaluator(),new Point(0,0),new Point(500,500));

//        objectAnimator.setEvaluator(new FallingBallEvaluator());
        objectAnimator.setInterpolator(new AccelerateInterpolator());
        objectAnimator.setDuration(3000);
    }

    private void initEva6() {
        propertyValuesHolder =  PropertyValuesHolder.ofObject("FallingPos",new FallingBallEvaluator(),new Point(0,0),new Point(500,500));
        objectAnimator1 = ObjectAnimator.ofPropertyValuesHolder(fallingImageView,propertyValuesHolder);

        objectAnimator1.setStartDelay(5000);
        objectAnimator1.setEvaluator(new FallingBallEvaluator());
        objectAnimator1.setInterpolator(new AccelerateInterpolator());
        objectAnimator1.setDuration(3000);
    }

    private void initEva2() {
        valueAnimator2 = ValueAnimator.ofInt(200,600);

        findViewById(R.id.tv_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                valueAnimator2.start();
//                startActivity(new Intent(FourActivity.this, FourActivity.class));
            }
        });

        valueAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int curValue = (int) animation.getAnimatedValue();
                tv2.layout(tv2.getLeft(),curValue,tv2.getRight(),curValue+tv2.getHeight());
            }
        });
        valueAnimator2.setDuration(1000);
//        valueAnimator.setInterpolator(new LinearInterpolator());
//        valueAnimator.setEvaluator(new MyEvaluator());
        valueAnimator2.setEvaluator(new ReverseEvaluator());
//        valueAnimator.setEvaluator(new ArgbEvaluator());
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
//            valueAnimator.setInterpolator(new BaseInterpolator() {
//                @Override
//                public float getInterpolation(float input) {
//                    return 0;
//                }
//            });
//        }
    }
}